<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item :label="tableText['jobsTime']">
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="tableText['reviewStage']">
          <el-select v-model="searchForm.stageId" clearable :placeholder="$t('riskReport.performance.prompt.reviewStage')">
            <el-option
              v-for="item in allStage"
              :key="`${item.id}_stage`"
              :label="item['flowName']"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="tableText['company']">
          <el-select v-model="searchForm.firmId" clearable :placeholder="$t('riskReport.performance.prompt.company')">
            <el-option
              v-for="item in allFirm"
              :key="`${item.id}_firm`"
              :label="item['firmName']"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="tableText['group']">
          <el-select v-model="searchForm.groupId" clearable :placeholder="$t('riskReport.performance.prompt.group')">
            <el-option-group
              v-for="group in allGroup"
              :key="group.value"
              :label="group.label"
            >
              <el-option
                v-for="item in group.options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-option-group>
          </el-select>
        </el-form-item>
        <el-form-item :label="tableText['account']">
          <el-input v-model="searchForm.riskEmployeeAccount" clearable :placeholder="$t('riskReport.performance.prompt.account')" :maxlength="50" />
        </el-form-item>
        <el-form-item :label="tableText['name']">
          <el-input v-model="searchForm.riskEmployeeName" clearable :placeholder="$t('riskReport.performance.prompt.name')" :maxlength="50" />
        </el-form-item>
        <el-form-item :label="tableText['borrow']">
          <el-select v-model="searchForm.firstLoan">
            <el-option
              v-for="item in tableText['borrowData']"
              :key="`${item.value}_borrow`"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
          <el-button :disabled="!(totalNumber > 0)" @click="exportList">
            {{ $t('common.ex') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%"
      >
        <el-table-column
          fixed="left"
          prop="date"
          :label="tableText['jobsTime']"
          min-width="120"
        />
        <el-table-column
          fixed="left"
          prop="riskEmployeeId"
          :label="tableText['id']"
          min-width="120"
        />
        <el-table-column
          fixed="left"
          prop="riskEmployeeAccount"
          :label="tableText['account']"
          min-width="120"
        />
        <el-table-column
          prop="groupName"
          :label="tableText['group']"
          min-width="120"
        />
        <el-table-column
          prop="stageName"
          :label="tableText['manualReviewStage']"
          min-width="120"
        />
        <el-table-column
          prop="companyName"
          :label="tableText['company']"
          min-width="120"
        />
        <el-table-column
          prop="companyType"
          :label="tableText['companyType']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ setValue(scope.row['companyType']) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="totalAllocateCount"
          :label="tableText['allocatedOrders']"
          min-width="120"
        />
        <el-table-column
          prop="auditCount"
          :label="tableText['reviews']"
          min-width="120"
        />
        <el-table-column
          prop="auditPassCount"
          :label="tableText['audits']"
          min-width="120"
        />
        <el-table-column
          prop="auditRefuseCount"
          :label="tableText['reviewRejection']"
          min-width="120"
        />
        <el-table-column
          prop="auditCloseCount"
          :label="tableText['reviewClosed']"
          min-width="120"
        />
        <el-table-column
          prop="unAuditCount"
          :label="tableText['unReviewed']"
          min-width="120"
        />
        <el-table-column
          prop="workTime"
          :label="tableText['workingHours']"
          min-width="120"
        />
        <el-table-column
          prop="hourAuditCount"
          :label="tableText['trialsTime']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row['hourAuditCount'] ? (scope.row['hourAuditCount']).toFixed(2) : 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="auditPassPercent"
          :label="tableText['auditPassRate']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row['auditPassPercent'] ? (scope.row['auditPassPercent']*100).toFixed(2) + '%' : 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="overdueRatio"
          :label="tableText['overdueRate']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row['overdueRatio'] ? (scope.row['overdueRatio']*100).toFixed(2) + '%' : 0 }}
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </el-row>
</template>

<script>
import { getPerformance, exportPerformance, getAllCreditReviewFirm, getAllCreditReviewGroup, getReview } from '@/api/risk'
import { formatDate } from '@/utils/method'
export default {
  name: 'Performance',
  data() {
    return {
      loading: true,
      tableData: [],
      allStage: [],
      allFirm: [],
      allGroup: [],
      time: '',
      tableText: {},
      totalNumber: 0,
      searchForm: this.params()
    }
  },
  mounted() {
    this.tableText = this.$t('riskReport.performance')
    this.getFirm()
    this.getStage()
    this.getData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        signInBeginDate: null,
        signInEndDate: null,
        stageId: null,
        firmId: null,
        groupId: null,
        riskEmployeeAccount: null,
        riskEmployeeName: null,
        firstLoan: ''
      }
    },
    getFirm() {
      getAllCreditReviewFirm()
        .then(res => {
          if (res.success) {
            this.allFirm = res.data.filter(item => {
              return item['status'] === 1
            })
            this.getGroup()
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    getGroup() {
      getAllCreditReviewGroup()
        .then(res => {
          if (res.success) {
            const arr = res.data.filter(item => {
              return item['status'] === 1
            })
            const groupOption = []
            arr.forEach(item => {
              if (!groupOption.includes(item['firmId'])) {
                groupOption.push(item['firmId'])
              }
            })
            const groupData = []
            groupOption.forEach(v => {
              const children = []
              const a = arr.filter(item => {
                return item['firmId'] === v
              })
              a.forEach(child => {
                children.push(
                  {
                    label: child['groupName'],
                    value: child['id']
                  }
                )
              })
              const name = this.allFirm.filter(aa => {
                return aa['id'] === v
              })
              const firmName = name && name[0] && name[0]['firmName'] ? name[0]['firmName'] : 0
              const id = name && name[0] && name[0]['id'] ? name[0]['id'] : 0
              groupData.push({
                label: firmName,
                value: id,
                options: children
              })
            })
            this.allGroup = groupData
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    getStage() {
      const data = {
        type: 1
      }
      getReview(data)
        .then(res => {
          if (res.success) {
            this.allStage = res.data.filter(item => {
              return item['status'] === 1
            })
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.time = ''
      this.getData()
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    getData() {
      this.loading = true
      getPerformance(this.searchData())
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    searchData() {
      const data = this.searchForm
      if (this.time && this.time.length > 1) {
        data['signInBeginDate'] = formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data['signInEndDate'] = formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
      } else {
        data['signInBeginDate'] = null
        data['signInEndDate'] = null
      }
      return data
    },
    exportList() {
      exportPerformance(this.searchData())
    },
    setValue(data) {
      const map = this.tableText['companyTypes']
      const name = map.filter(item => {
        return item.value.toString() === data.toString()
      })
      return (name.length > 0 && name[0].desc) ? name[0].desc : ''
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin: 10px;
}
</style>
